/**
 * 打印样式工具函数
 * 用于统一预览和打印的样式，确保两者完全一致
 */

/**
 * 生成统一的页面样式对象
 * @param {Object} formatSettings - 格式设置对象
 * @param {boolean} forPrint - 是否为打印样式
 * @returns {Object} 样式对象
 */
export const generatePageStyles = (formatSettings, forPrint = false) => {
  const baseStyles = {
    page: {
      width: '210mm',
      minHeight: '297mm',
      margin: forPrint ? '0' : '20px auto',
      padding: `${formatSettings.pageMargin}cm`,
      fontSize: `${formatSettings.fontSize}pt`,
      lineHeight: formatSettings.lineHeight,
      fontFamily: formatSettings.fontFamily,
      backgroundColor: 'white',
      boxShadow: forPrint ? 'none' : '0 0 10px rgba(0,0,0,0.1)',
      border: forPrint ? 'none' : '1px solid #ddd',
      position: 'relative',
      boxSizing: 'border-box',
      textAlign: 'left'
    },
    pageContent: {
      width: '100%',
      height: '100%',
      position: 'relative'
    },
    recordLine: {
      minHeight: `${parseFloat(formatSettings.fontSize) * parseFloat(formatSettings.lineHeight) * 1.33}px`,
      paddingBottom: '2px',
      marginBottom: formatSettings.lineSpacing || '8mm',
      lineHeight: formatSettings.lineHeight,
      fontSize: `${formatSettings.fontSize}pt`,
      fontFamily: formatSettings.fontFamily,
      display: 'block',
      width: '100%',
      boxSizing: 'border-box',
      textAlign: 'left'
    }
  }

  if (forPrint) {
    baseStyles.page.height = '297mm'
    baseStyles.page.pageBreakAfter = 'auto'
    baseStyles.page.pageBreakInside = 'avoid'
  }

  return baseStyles
}

/**
 * 生成统一的CSS样式字符串
 * @param {Object} formatSettings - 格式设置对象
 * @param {boolean} forPrint - 是否为打印样式
 * @returns {string} CSS样式字符串
 */
export const generateCSSString = (formatSettings, forPrint = false) => {
  const styles = generatePageStyles(formatSettings, forPrint)
  
  return `
    ${forPrint ? `
      @page {
        size: A4;
        margin: 0;
      }
      body {
        margin: 0;
        padding: 0;
        font-family: ${formatSettings.fontFamily};
        font-size: ${formatSettings.fontSize}pt;
        line-height: ${formatSettings.lineHeight};
      }
    ` : ''}
    
    /* 页面基础样式 */
    .page {
      width: ${styles.page.width} ${forPrint ? '!important' : ''};
      ${forPrint ? `height: ${styles.page.height} !important;` : `min-height: ${styles.page.minHeight};`}
      margin: ${styles.page.margin} ${forPrint ? '!important' : ''};
      padding: ${styles.page.padding} ${forPrint ? '!important' : ''};
      box-shadow: ${styles.page.boxShadow} ${forPrint ? '!important' : ''};
      border: ${styles.page.border} ${forPrint ? '!important' : ''};
      position: ${styles.page.position};
      box-sizing: ${styles.page.boxSizing};
      background: ${styles.page.backgroundColor};
      font-family: ${styles.page.fontFamily};
      font-size: ${styles.page.fontSize};
      line-height: ${styles.page.lineHeight};
      text-align: ${styles.page.textAlign};
      ${forPrint && styles.page.pageBreakAfter ? `page-break-after: ${styles.page.pageBreakAfter};` : ''}
      ${forPrint && styles.page.pageBreakInside ? `page-break-inside: ${styles.page.pageBreakInside};` : ''}
      ${!forPrint ? 'transition: all 0.3s;' : ''}
    }
    
    /* 页面内容区域 */
    .page-content {
      width: ${styles.pageContent.width};
      height: ${styles.pageContent.height};
      position: ${styles.pageContent.position};
    }
    
    /* 记录行样式 */
    .record-line {
      min-height: ${styles.recordLine.minHeight};
      padding-bottom: ${styles.recordLine.paddingBottom};
      margin-bottom: ${styles.recordLine.marginBottom};
      line-height: ${styles.recordLine.lineHeight};
      font-size: ${styles.recordLine.fontSize};
      font-family: ${styles.recordLine.fontFamily};
      display: ${styles.recordLine.display};
      width: ${styles.recordLine.width};
      box-sizing: ${styles.recordLine.boxSizing};
      text-align: ${styles.recordLine.textAlign};
    }
    
    .record-line.solid {
      border-bottom: 1px solid #333;
    }
    .record-line.dashed {
      border-bottom: 1px dashed #333;
    }
    .record-line.none {
      border-bottom: none;
    }
    
    .record-line h3 {
      border-bottom: none !important;
      text-align: center;
      margin: 0;
      text-indent: 0 !important;
      font-family: ${formatSettings.fontFamily};
      font-size: ${formatSettings.fontSize}pt;
    }
    
    p {
      margin: 0;
      padding: 0;
      font-family: ${formatSettings.fontFamily};
      font-size: ${formatSettings.fontSize}pt;
      line-height: ${formatSettings.lineHeight};
    }
    
    h3 {
      font-family: ${formatSettings.fontFamily};
      font-size: ${formatSettings.fontSize}pt;
      text-align: center;
      margin: 0;
      border-bottom: none !important;
      text-indent: 0 !important;
    }
    
    .record-line:empty::after {
      content: " ";
      visibility: hidden;
    }
    
    /* 页码样式 */
    .page-number {
      position: absolute;
      font-size: 13px;
      color: #666;
      z-index: 10;
      background: rgba(255,255,255,0.8);
      padding: 2px 10px;
      border-radius: 4px;
      user-select: none;
    }
    .page-number-top-left {
      top: 8px;
      left: 16px;
    }
    .page-number-top-right {
      top: 8px;
      right: 16px;
    }
    .page-number-bottom-left {
      bottom: 8px;
      left: 16px;
    }
    .page-number-bottom-right {
      bottom: 8px;
      right: 16px;
    }
    
    /* 询问次数样式 */
    .inquiry-count {
      position: absolute;
      font-size: 13px;
      color: #666;
      z-index: 10;
      background: rgba(255,255,255,0.8);
      padding: 2px 10px;
      border-radius: 4px;
      user-select: none;
    }
    .inquiry-count-top-right {
      top: 32px;
      right: 16px;
    }
    
    /* 签名栏样式 */
    .signature-bar {
      position: absolute;
      bottom: 24px;
      display: flex;
      font-size: 14px;
      color: #333;
      z-index: 10;
      align-items: center;
      pointer-events: none;
      user-select: none;
    }
    .signature-bottom-left {
      left: 24px;
      right: 24px;
      justify-content: flex-start;
    }
    .signature-bottom-right {
      right: 60px;
      width: 180px;
      justify-content: flex-end;
    }
    .signature-label {
      min-width: 80px;
      flex-shrink: 0;
      margin-right: 12px;
    }
    .signature-flex-spacer {
      flex: 1 1 auto;
    }
    .signature-date {
      font-size: 13px;
      color: #666;
      min-width: 120px;
      text-align: right;
      letter-spacing: 2px;
    }
    
    ${!forPrint ? `
      /* 预览专用样式 */
      .page.current-page {
        border-color: #409eff;
        box-shadow: 0 6px 12px rgba(64, 158, 255, 0.2);
      }
    ` : `
      /* 打印专用样式 */
      .current-page {
        border: none !important;
        box-shadow: none !important;
      }
      
      .page:not(:last-child) {
        page-break-after: always;
      }
    `}
  `
}

/**
 * 计算行距样式值
 * @param {string|number} lineSpacing - 行距设置
 * @returns {string} CSS行距值
 */
export const computeLineSpacing = (lineSpacing) => {
  if (typeof lineSpacing === 'number') return lineSpacing + 'px'
  if (typeof lineSpacing === 'string') return lineSpacing
  return '8mm'
}
